package com.example.swd1.gudong.main.user.myrecord;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.view.ReFreshView;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserRecordActivity extends Activity implements View.OnClickListener, ReFreshView.OnRefreshListener {
    private ReFreshView lv_user_record;
    private ImageView iv_user_record_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_record);
        lv_user_record = (ReFreshView) findViewById(R.id.lv_user_record);
        iv_user_record_back = (ImageView)findViewById(R.id.iv_user_record_back);

        iv_user_record_back.setOnClickListener(this);
        UserRecordAdapter adapter = new UserRecordAdapter(this, loadUserRecordData());
        lv_user_record.setAdapter(adapter);
        lv_user_record.setOnRefreshListener(this);
    }

    private List<UserRecord> loadUserRecordData() {
        List<UserRecord> data = new ArrayList<>();
        UserRecord record = new UserRecord(R.string.user_record_title, "2016年12月12日", 12.00);
        data.add(record);
        record = new UserRecord(R.string.user_record_3km, "2016年12月12日", 23.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_5km, "2016年12月12日", 24.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_10km, "2016年12月12日", 25.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_half_marathon, "2016年12月12日", 26.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_full_marathon, "2016年12月12日", 27.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_most_far_run, "2016年12月12日", 28.12);
        data.add(record);
        record = new UserRecord(R.string.user_record_most_far_walk, "2016年12月12日", 0);
        data.add(record);
        return data;
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    public class UserRecord {
        private int titleId;
        private String date;
        private String distance;
        private boolean isEmpty;

        public UserRecord(int titleId, String date, double distance) {
            if (distance == 0) {
                this.distance = "--:--:";
            } else {
                this.distance = distance + "";
            }
            this.date = date;
            this.titleId = titleId;


            if (distance == 0) {
                isEmpty = true;
            }
        }

        public int getTitleId() {
            return titleId;
        }

        public String getDate() {
            return isEmpty ? "未完成" : date;
        }

        public String getDistance() {
            return distance;
        }

        public String getKm() {
            return isEmpty ? "--" : "km";
        }
    }

    /*-begin-20160913-shile.wu-add-for-task2781345-*/
    private mHandler mHandler = new mHandler();
    @Override
    public void onRefresh() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                lv_user_record.setOnRefreshComplete();
            }
        }
    }
    /*-end-20160913-shile.wu-add-for-task2781345-*/
}
